package com.green.common.page.grid.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author lineshow email:lineshow7@gmail.com
 * 
 *         PageQueryHanlder.java created on 2011-7-16 | modified on 2011-10-25
 * 
 *         description: query grid data approach db
 */
public class PageQueryHanlder {

	public static final String MODE_QL = "ql";
	public static final String MODE_HQL = "hql";
	public static final String MODEL_SQL = "sql";

	private final StringBuilder tempql;
	private String ql;
	private String rowsCountQl;
	private final List<Object> conditionList;
	private final Map<String, Object> conditionMap;
	private String qlmode;

	public PageQueryHanlder() {
		tempql = new StringBuilder();
		conditionList = new ArrayList<Object>();
		conditionMap = new HashMap<String, Object>();
	}

	public void setQl(StringBuilder ql) {
		this.ql = ql.toString();
	}

	public String getQl() {
		return ql;
	}

	public String getRowsCountQl() {
		return rowsCountQl;
	}

	/**
	 * obtain all query params
	 * 
	 * @return
	 */
	public List<Object> getParams() {
		return conditionList;
	}

	public Map<String, Object> getParamsInMap() {
		return conditionMap;
	}

	public void addParamInMap(String queryKey, Object queryValue) {
		conditionMap.put(queryKey, queryValue);
	}

	/**
	 * set mode ql or sql etc.
	 * 
	 * @param qlmode
	 */
	public void setQlMode(String qlmode) {
		this.qlmode = qlmode;
	}

	/**
	 * add query language segment.
	 * 
	 * @param segment
	 * @return
	 */
	public PageQueryHanlder addQlSegment(String segment) {
		tempql.append(segment);
		return this;
	}

	public PageQueryHanlder addQlSegment(StringBuilder segment) {
		return addQlSegment(segment.toString());
	}

	public void internalDeal(ConditionContainer conditionContainer) {
		this.rowsCountQl = this.tempql.toString();
		this.addQlSegment(" order by ")
				.addQlSegment(
						org.apache.commons.lang.StringUtils
								.join(conditionContainer.getSortItem()))
				.addQlSegment(" ")
				.addQlSegment(conditionContainer.getSortType());
		this.ql = this.tempql.toString();
	}

	/**
	 * add query param [each param correspond with '?'].
	 * 
	 * @param queryParam
	 */
	public void addParam(Object queryParam) {
		conditionList.add(queryParam);
	}
//
//	/**
//	 * query necessary data serving the grid of page.
//	 * 
//	 * @param <T>
//	 * 
//	 * @return
//	 */
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public PageContainer loadOut(HibernateDao hibernateDao, ConditionContainer conditionContainer) {
//		internalDeal(conditionContainer);
//		PageContainer pageContainer = new PageContainer();
//
//		Integer totalRowsCount = null;
//		if (CollectionUtils.isNotEmpty(conditionList)) {
//			totalRowsCount = hibernateDao.getTotalRowCount(rowsCountQl, conditionList, qlmode);
//		} else {
//			totalRowsCount = hibernateDao.getTotalRowCount(rowsCountQl, conditionMap, qlmode);
//		}
//		pageContainer.setTotalCount(totalRowsCount);
//		if (totalRowsCount != 0) {
//			List<Map<String, Object>> dataList = null;
//			if (this.qlmode.equals(PageQueryHanlder.MODEL_SQL)) {
//				if (CollectionUtils.isNotEmpty(conditionList)) {
//					dataList = hibernateDao.getGridDataInSql(ql, conditionList, conditionContainer.getStartIndex(),
//							conditionContainer.getMaxIndex());
//				} else {
//					dataList = hibernateDao.getGridDataInSql(ql, conditionMap, conditionContainer.getStartIndex(),
//							conditionContainer.getMaxIndex());
//				}
//			} else {
//				if (CollectionUtils.isNotEmpty(conditionList)) {
//					dataList = hibernateDao.getGridDataInQl(ql, conditionList, conditionContainer.getStartIndex(),
//							conditionContainer.getMaxIndex());
//				} else {
//					dataList = hibernateDao.getGridDataInQl(ql, conditionMap, conditionContainer.getStartIndex(),
//							conditionContainer.getMaxIndex());
//				}
//			}
//			Grid grid = new Grid();
//			grid.setData(dataList);
//			pageContainer.setGrid(grid);
//		} else {
//			Grid grid = new Grid();
//			grid.setData(new ArrayList<Map<String, Object>>());
//			pageContainer.setGrid(grid);
//		}
//		return pageContainer;
//	}

}
